package com.jn.retrofit.views;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

import com.jn.retrofit.R;
import com.jn.retrofit.entity.DictionaryResponse;
import com.jn.retrofit.entity.HotSearchResponse;
import com.jn.retrofit.service.ApiService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DataDisplayActivity extends AppCompatActivity {
    private TextView resultTextView;
    private EditText inputEditText;
    private Button fetchButton;
    private static final String TAG = "DataDisplayActivity";
    private ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_display);

        resultTextView = findViewById(R.id.text_view_result);
        inputEditText = findViewById(R.id.input_edit_text);
        fetchButton = findViewById(R.id.fetch_button);

        // 创建 Retrofit 实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.oioweb.cn/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiService = retrofit.create(ApiService.class);

        fetchButton.setOnClickListener(v -> {
            String inputText = inputEditText.getText().toString().trim();
            if (!inputText.isEmpty()) {
                fetchDictionaryData(inputText);
            } else {
                Toast.makeText(DataDisplayActivity.this, "请输入要查询的字", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void fetchDictionaryData(String text) {
        apiService.fetchDictionaryData(text).enqueue(new Callback<DictionaryResponse>() {
            @Override
            public void onResponse(Call<DictionaryResponse> call, Response<DictionaryResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    DictionaryResponse dictionaryResponse = response.body();
                    if (dictionaryResponse.getCode() == 200) {
                        StringBuilder result = new StringBuilder();
                        result.append("汉字: ").append(dictionaryResponse.getResult().getHanzi()).append("\n")
                                .append("拼音: ").append(dictionaryResponse.getResult().getPinyin()).append("\n")
                                .append("解释: ").append(String.join(", ", dictionaryResponse.getResult().getBasicExplain()));

                        resultTextView.setText(result.toString());
                    } else {
                        Toast.makeText(DataDisplayActivity.this, "获取字典数据失败", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(DataDisplayActivity.this, "获取字典数据失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<DictionaryResponse> call, Throwable t) {
                Log.e(TAG, "请求失败: ", t);
                Toast.makeText(DataDisplayActivity.this, "请求失败，请稍后重试。", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
